package com.efrei.schoolmanager.views;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.efrei.schoolmanager.controlers.ControleurEleve;
import com.efrei.schoolmanager.controlers.ControleurEleves;
import com.efrei.schoolmanager.exceptions.LogicException;
import com.efrei.schoolmanager.model.ModeleListeEleves;
import com.efrei.schoolmanager.model.obj.Eleve;
import com.efrei.schoolmanager.tools.Tools;
import com.efrei.schoolmanager.views.components.CustomButton;

/**
 * Vue de listage des eleves de l'application
 * 
 */
public class VueListerEleves extends JPanel {
	private static final long serialVersionUID = -5899613307536099293L;

	private final static String TITLE = "Liste des Eleves";

	private final ControleurEleves controleur;

	// Proprietaire de cette dialog(objectif: rendre modale la jdialog)
	private final VueGenerale proprio;

	// JPanels
	private JPanel tablePanel, titlePanel, buttonPanel;

	// JTable
	private JTable infoElevesTable;

	// JButtons
	private CustomButton voirModifierEleveButton, supprimerEleveButton,
			ajouterEleveButton;

	// JLabels
	private JLabel listeClasseLabel;

	public List<Eleve> eleves;

	private ModeleListeEleves modeleEleveTable;

	public VueListerEleves(final VueGenerale proprio,
			ControleurEleves controleur) {
		this.proprio = proprio;
		this.controleur = controleur;

		new Thread(new Runnable() {
			@Override
			public void run() {
				boolean error = false;
				try {
					// Chargement de l'écran
					initLabel();
					initButtons();
					initTable();
					remplirTitlePanel();
					remplirTablePanel();
					remplirButtonPanel();
					configurerVue();

				} catch (final Exception e) {
					// En cas d'erreur, on efface tout
					error = true;
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							proprio.displayError(e);
							proprio.removePanel();
							proprio.stopBarreChargement();
						}
					});
				}
				if (!error)
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							proprio.stopBarreChargement();
						}
					});
			}
		}).start();
		proprio.startBarreChargement();
	}

	/**
	 * Configurer les options de la vue
	 * 
	 */
	private void configurerVue() {
		setLayout(new BorderLayout());
		this.add(titlePanel, BorderLayout.NORTH);
		this.add(tablePanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		setVisible(true);
	}

	private void initButtons() {
		voirModifierEleveButton = new CustomButton("Voir/Modifier l'eleve");
		voirModifierEleveButton.setEnabled(false);
		voirModifierEleveButton
				.addActionListener(new ModifierEleveActionListener());
		ajouterEleveButton = new CustomButton("Ajouter un eleve");
		ajouterEleveButton.addActionListener(new AjouterEleveActionListener());
		supprimerEleveButton = new CustomButton("Supprimer l'eleve");
	}

	private void remplirTitlePanel() {
		titlePanel = new JPanel();
		titlePanel.add(listeClasseLabel);
	}

	private void remplirTablePanel() {
		tablePanel = new JPanel(new BorderLayout());
		tablePanel.add(infoElevesTable.getTableHeader(), BorderLayout.NORTH);
		tablePanel.add(new JScrollPane(infoElevesTable), BorderLayout.CENTER);
	}

	private void remplirButtonPanel() {
		buttonPanel = new JPanel();
		buttonPanel.add(ajouterEleveButton);
		buttonPanel.add(voirModifierEleveButton);
		buttonPanel.add(supprimerEleveButton);

	}

	private void initTable() {
		modeleEleveTable = new ModeleListeEleves(controleur);
		infoElevesTable = new JTable(modeleEleveTable);
		infoElevesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ListSelectionModel listSelectionModel = infoElevesTable
				.getSelectionModel();
		listSelectionModel
				.addListSelectionListener(new ListeEleveTableListSelectionListener());
	}

	private void initLabel() {
		listeClasseLabel = new JLabel(TITLE);
		listeClasseLabel.setFont(new Font("Arial", Font.BOLD, 20));
	}

	/* Listeners */
	class AjouterEleveActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			ControleurEleve ce = null;
			try {
				ce = new ControleurEleve(null);
			} catch (LogicException e1) {
				Tools.displayError(e1.getMessage(), "Erreur");
			} catch (Exception e2) {
				Tools.displayError("Erreur de chargement, voir logs", "Erreur");
			}
			if (ce != null) {
				VueAjoutEleve vae = new VueAjoutEleve(proprio, ce);
				proprio.configurerContenu(vae);
			}
		}

	}

	class ModifierEleveActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ControleurEleve cae = null;
			try {
				cae = new ControleurEleve(
				// Recuperer l'eleve dans la liste
						modeleEleveTable.getEleveAtRow(infoElevesTable
								.getSelectedRow()));
			} catch (LogicException e1) {
				Tools.displayError(e1.getMessage(), "Erreur");
			} catch (Exception e2) {
				Tools.displayError("Erreur de chargement, voir logs", "Erreur");
			}
			if (cae != null) {
				// Effacer l'ecran pour le nouveau
				VueModifierEleve vae = new VueModifierEleve(proprio, cae);
				proprio.configurerContenu(vae);
			}
		}
	}

	class ListeEleveTableListSelectionListener implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (infoElevesTable.getSelectedRow() != -1) {
				voirModifierEleveButton.setEnabled(true);
			} else {
				voirModifierEleveButton.setEnabled(false);
			}
		}

	}

}
